<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends AdminController {

	public function adminList(){
		$data=M('admin_user')->select();
		$this->assign('_list',$data);
		$this->display();

	}

	public function addAdmin(){
		$this->display();
	}

	//角色列表
    public function roleList(){
    	$data=M('admin_role')->select();
		$this->assign('_list',$data);
		$this->display();
    }

    //添加角色
    public function addRole(){
    	if(IS_POST){
    		$data=array(
    				'name'=>I('rolename'),
    				'status'=>I('status'),
    				'remark'=>I('remark'),
    				);
    		M('admin_role')->add($data);
    		$this->success('添加角色成功',U('Admin/Rights/roleList'));
    	}
    	$this->display();
    }

    /**
     * 分配权限
     */
    public function roleNode(){
    	$id=I('get.id');
        $roleList = M('admin_role')->field('id,name')->select();
        empty($id) and $rolelist[0]['id'];

    	$data=M('admin_node')->select();
    	$node_list=array();
    	foreach($data as $v){
    		if($v['level'] == 2){
    			$node_list[$v['id']]['id']=$v['id'];
    			$node_list[$v['id']]['title']=$v['title'];
    		}elseif($v['level'] == 3){
    			$node_list[$v['pid']]['operator'][$v['id']]['id']=$v['id'];
    			$node_list[$v['pid']]['operator'][$v['id']]['title']=$v['title'];
    		}
    	}
    	$node_list2[]=array(
    			'id'=>1,
    			'title'=>'后台管理',
    			'child'=>$node_list,
    	);
    	//dump($node_list2);
    	$this->assign('rolelist',$roleList);
    	$this->assign('id',$id);
    	$this->assign('node_list',$node_list2);
    	$this->display();
    }

    //节点列表
    public function nodeList(){
    	$data=M('admin_node')->select();
    	$this->assign('_list',$data);
    	$this->display();
    }

    //添加节点
    public function addNode(){
    	if(IS_POST){
    		$data=array(
    				'name' => I('post.name'),
    				'title' => I('post.title'),
    				'status' => I('post.status','1','intval'),
    				'remark' => I('post.remark'),
    				'sort' => I('post.sort','0','intval'),
    				'pid' => I('post.pid','1','intval'),
    				'level' => I('post.level','2','intval'),
    				'menu_id' => I('post.menu_id','1','intval'),
    				'is_menu' => I('post.is_menu','1','intvale'),
    				'path' => I('post.path'),
    				);
    		$nid=I('post.nid','','intval');
    		if(isset($_POST['nid'])){
				$edit=M('admin_node')->where(array('id'=>$nid))->data($data)->save();
				if($edit){
					$this->success('修改成功',U('System/nodeList'));
				}else{
					$this->error('修改失败');
				}
    		}else{
	    		$add=M('admin_node')->data($data)->add();
	    		if($add){
	    			$this->success('添加成功',U('System/nodeList'));
	    		}else{
	    			$this->error('添加失败');
	    		}
    		}
    	}else{
    		$nid=I('get.nid','','intval');
    		if(!empty($nid)){
				$list=M('admin_node')->find($nid);
				//dump($list);
				$this->assign('_list',$list);
				$this->assign('nid',$nid);
    		}
    		$menu = M('admin_menu')->where(array('status'=>1))->select();//dump($menu);
    		$this->assign('menu',$menu);
    		$this->display();
    	}
    }

    public function editNodeStatus(){
    	$nid=I('get.nid','','intval');
    	empty($nid) && $this->error('参数错误');
    	$row=M('admin_node')->find($nid);
    	if(empty($row)){
    		$this->error('该节点不存在');
    	}else{
    		$sts=I('get.type','','trim');
    		$status=$row['status'];
    		if($sts=='lock'){
    			$status=0;
    		}elseif($sts=='unlock'){
    			$status=1;
    		}else{
    			$this->error('参数错误');
    		}
    		$editlock=M('admin_node')->where(array('id'=>$nid))->data(array('status'=>$status))->save();
    		if($editlock){
    			$this->success('操作成功');
    		}else{
    			$this->error('操作失败');
    		}
    	}
    }

    public function delNode(){
    	$nid=I('get.nid','','intval');
    	empty($nid) && $this->error('参数错误');
    	$del=M('admin_node')->where(array('id'=>$nid))->delete();
    	if($del){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
    }

    public function menuList(){
    	$data=M('admin_menu')->select();
    	$this->assign('_list',$data);
    	$this->display();
    }

    //添加角色
    public function addMenu(){
    	if(IS_POST){
    		$data=array(
    				'name'=>I('menuname'),
    				'status'=>I('status','0','intval'),
    		);
    		M('admin_menu')->add($data);
    		$this->success('添加顶部菜单成功',U('Admin/System/menuList'));
    	}
    	$this->display();
    }
}